/**
 * HOMER Core - an open and flexible OSGi-based software platform for SmartHome and AAL applications
 *
 * Copyright (C) 2011-2013 AIT Austrian Institute of Technology GmbH - <http://www.ait.ac.at>
 * Contact: Thomas Fuxreiter <thomas.fuxreiter@ait.ac.at>
 *
 * This file is part of HOMER Core.
 *
 * HOMER Core is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * HOMER Core is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package at.aal2aac.eventbusclient;

import java.util.Calendar;
import java.util.Date;

public final class Utils {

	public static final transient double SECSOFDAY = 86400;

    private Utils() {
    }
	
	/**
	 * 	Returns a class by name from the homer.core.common name space
	 *  @TODO replace the call by the class loader for direct search 
	 *  		currently used by {@link at.ac.ait.hbs.homer.core.eventbus.server.JsonUtils}
	 * @param className
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static Class<?> getClassByName(final String className) throws ClassNotFoundException {
		return Class.forName(className);
	}

	
	/**
	 * Returns the time of a day in seconds for the given date.
	 * @param date - the date as {@link Date}
	 * @return
	 */
	public static long getSecondsOfDay(final Date date) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(date.getTime());
		return getSecondsOfDay(c);
	}

	
	/**
	 * Returns the the time of a day in seconds for the given date.
	 * @param c - the date as {@link Calendar}
	 * @return
	 */
	public static long getSecondsOfDay(final Calendar c) {
		long secs;
		secs = c.get(Calendar.HOUR_OF_DAY) * 60 * 60;
		secs += c.get(Calendar.MINUTE) * 60;
		secs += c.get(Calendar.SECOND);

		return secs;
	}
   
	/**
	 * Returns a Calendar instance of the current day with the time of the day given in seconds.
	 * @param secondsOfDay - time of day in seconds
	 * @return 
	 */
	
	public static Calendar getTodayFromSeconds(final Long secondsOfDay) {
		Calendar cal = Calendar.getInstance();

		Integer h = Math.round(secondsOfDay / 3600f);
		Integer m = Math.round((secondsOfDay - h * 3600) / 60f);
		Integer s = Math.round(secondsOfDay % 60f);
		cal.set(Calendar.HOUR_OF_DAY, h);
		cal.set(Calendar.MINUTE, m);
		cal.set(Calendar.SECOND, s);

		return cal;
	}

}


